#ifndef __COMMON_H__
#define __COMMON_H__
#include <ra_types.h>
#include <c_types.h>
#include <ets_sys.h>
#include <os_type.h>
#include <osapi.h>
#include <mem.h>

#define memcpy		ets_memcpy
#define memset		ets_memset
#define memcmp		ets_memcmp
#define memchr		lib_memchr
#define strcmp		ets_strcmp
#define strlen		ets_strlen
#define strnlen		lib_strnlen
#define strncmp		ets_strncmp
#define sprintf		ets_sprintf
#define printf		ets_printf
#define strcpy		ets_strcpy
#define strncpy		ets_strncpy
#define strchr		ets_strchr
#define strstr		ets_strstr
#define strcat		ets_strcat

#define malloc	os_malloc
#define realloc	os_realloc
#define zalloc	os_zalloc
#define free	os_free

#define LOGFORMAT	"[log]%s:%d: "
#define PROMPT		__FUNCTION__

#define TIMER_REGISTER(P_TIMER,FUNC,PARAM,TIME,CYCLE)	do \
														{ \
															os_timer_disarm(P_TIMER); \
															os_timer_setfn(P_TIMER, (os_timer_func_t *)FUNC, PARAM); \
															os_timer_arm(P_TIMER, TIME, CYCLE); \
														}while(0)
#define debug_print(format, ...) \
								do{ \
									printf(LOGFORMAT, PROMPT, __LINE__); \
									printf(format,##__VA_ARGS__); \
									printf("\n"); \
								} while (0)

#define ERR_MSG(format , ...) \
							do{ \
								printf("[ERROR]%s:%d: ", PROMPT, __LINE__); \
								printf(format,##__VA_ARGS__); \
								printf("\n"); \
							} while (0)

#define ASSERT(exp) \
					do{ \
						if((exp)) \
						{ \
							ERR_MSG(#exp); \
							goto fail; \
						} \
					}while(0)

#define ASSERT_EVAL(exp, var, code) \
						do{ \
							if(exp) \
							{ \
								ERR_MSG(#exp); \
								var = code; \
								goto fail; \
							} \
						}while(0)

#define COR_LINE __LINE__

#define scr_begin         static unsigned int scrLine = 0; switch(scrLine) { case 0:;
#define scr_init(timer,func,arg) os_timer_disarm(timer),os_timer_setfn(timer, (os_timer_func_t *)func, arg)
#define scr_yield_noret       \
        do {\
            scrLine=COR_LINE;\
            return; case COR_LINE:;\
        } while (0)
#define scr_delay_ms(timer,time)	os_timer_arm(timer, time, 0);scr_yield_noret
#define scr_finish_noret       } scrLine=0;return


#endif